//
//  LXRequstOrderTableViewCell.m
//  TuDianEducation
//
//  Created by lixinkeji on 2020/6/28.
//  Copyright © 2020 zhangbenchao. All rights reserved.
//

#import "LXRequstOrderTableViewCell.h"
#import "BkMessageOrderView.h"
@interface LXRequstOrderTableViewCell()

@property (nonatomic, strong) UIView *bacView;
@property (nonatomic, strong) UILabel *orderNumLabel;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UIView *septorLine;
@property (nonatomic,strong) UILabel *bkNameLabel;
@property (nonatomic,strong) UILabel *moneyLabel;
@property (nonatomic,strong) UILabel *sevendaysLabel;
@property (nonatomic,strong) UILabel *amountLabel;
@property (nonatomic,strong) UIView *bottomLine;
@property (nonatomic,strong) UIButton *confirmDoneBtn;
@property (nonatomic,strong) UILabel *totalAmountLabel;
@property (nonatomic,strong) UILabel *totalLabel;
@property (nonatomic,strong) UIImageView *headerImgView;
@property (nonatomic,strong) UIButton *cancellBtn;

@property (nonatomic,strong) NSArray *messageArray;
@property (nonatomic,strong) NSMutableArray *uiMessageArray;

@property (nonatomic,strong) UIButton *applyMoneyBackBtn;
@end

@implementation LXRequstOrderTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self viewConfig];
    }
    return self;
}
-(void)viewConfig
{   self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = kMainBgColor;
    [self.contentView addSubview:self.bacView];
    [self.bacView addSubview:self.orderNumLabel];
    [self.bacView addSubview:self.statusLabel];
    [self.bacView addSubview:self.septorLine];
//    [self.bacView addSubview:self.headerImgView];
//    [self.bacView addSubview:self.bkNameLabel];
//    [self.bacView addSubview:self.moneyLabel];
//    [self.bacView addSubview:self.sevendaysLabel];
//    [self.bacView addSubview:self.amountLabel];
     [self.bacView addSubview:self.bottomLine];
    [self.bacView addSubview:self.totalAmountLabel];
    [self.bacView addSubview:self.totalLabel];
    
    [self.bacView addSubview:self.confirmDoneBtn];
    [self.bacView addSubview:self.cancellBtn];
    [self.bacView addSubview:self.applyMoneyBackBtn];
    
}
-(UIView *)bacView{
    if (!_bacView) {
        _bacView = [[UIView alloc]initWithFrame:CGRectMake(TSScaleW(15), TSScaleW(10), TSScaleW(345), TSScaleW(230))];
        _bacView.backgroundColor = kWhiteColor;
        [_bacView setCornerRadius:TSScaleW(10)];
    }
    return _bacView;
}
-(UILabel *)orderNumLabel{
    if (!_orderNumLabel) {
        _orderNumLabel = [TSWLTools allocLabel:@"订单编号：20201525452" font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(15), TSScaleW(17), _bacView.width - TSScaleW(15)*2, TSScaleW(12)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _orderNumLabel;
}
-(UILabel *)statusLabel{
    if (!_statusLabel) {
        _statusLabel = [TSWLTools allocLabel:@"已完成" font:systemFont(TSScaleW(13)) textColor:kGreenColor frame:CGRectMake(TSScaleW(15), TSScaleW(17), _bacView.width - TSScaleW(15)*2, TSScaleW(13)) textAlignment:(NSTextAlignmentRight)];
    }
    return _statusLabel;
}
-(UIView *)septorLine
{
    if (!_septorLine) {
        _septorLine = [[UIView alloc]initWithFrame:CGRectMake(0, TSScaleW(14) + _orderNumLabel.bottom, _orderNumLabel.width, .5)];
        _septorLine.backgroundColor = kMainLineColor;
    }
    return _septorLine;
}
-(UIImageView *)headerImgView{
    if (!_headerImgView) {
        _headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(TSScaleW(15), TSScaleW(15) + _septorLine.bottom, TSScaleW(47), TSScaleW(63))];
        _headerImgView.backgroundColor = kGrayTxtColor;

    }
    return _headerImgView;
}
-(UILabel *)bkNameLabel{
    if (!_bkNameLabel) {
        _bkNameLabel = [TSWLTools allocLabel:@"初中数学运算大法" font:systemFont(TSScaleW(14)) textColor:kMainTxtColor frame:CGRectMake(_headerImgView.right + TSScaleW(10),  _septorLine.bottom + TSScaleW(18), TSScaleW(210), TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
        //_bkNameLabel.numberOfLines = 0;
    }
    return _bkNameLabel;
}
-(UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [TSWLTools allocLabel:@"￥999.00" font:systemFont(TSScaleW(12)) textColor:kMainTxtColor frame:CGRectMake(_headerImgView.right + TSScaleW(10), _bkNameLabel.top, TSScaleW(260), TSScaleW(15)) textAlignment:(NSTextAlignmentRight)];
        NSString *string = _moneyLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(1,  string.length - 4)];
        _moneyLabel.attributedText = atts;
    }
    return _moneyLabel;
}
-(UILabel *)sevendaysLabel{
    if (!_sevendaysLabel) {
        _sevendaysLabel = [TSWLTools allocLabel:@"7天无理由退换" font:systemFont(10) textColor:UIColorFromRGB(0xD95F35) frame:CGRectMake(_bkNameLabel.left, TSScaleW(9) + _bkNameLabel.bottom, TSScaleW(80), TSScaleW(15)) textAlignment:(NSTextAlignmentCenter)];
        _sevendaysLabel.backgroundColor = UIColorFromRGB(0xFFF1F1);
        _sevendaysLabel.cornerRadius = _sevendaysLabel.height/2.f;
        
    }
    return _sevendaysLabel;
}
-(UILabel *)amountLabel{
    if (!_amountLabel) {
        _amountLabel = [TSWLTools allocLabel:@"x1" font:systemFont(12) textColor:kGrayTxtColor frame:CGRectMake(_moneyLabel.left, TSScaleW(37) + _moneyLabel.bottom, _moneyLabel.width , TSScaleW(12)) textAlignment:(NSTextAlignmentRight)];
    }
    return _amountLabel;
}
-(UIView *)bottomLine
{
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc]initWithFrame:CGRectMake(_headerImgView.left, TSScaleW(13) + _headerImgView.bottom, TSScaleW(318), .5)];
        _bottomLine.backgroundColor = kMainLineColor;
       }
       return _bottomLine;
}
-(UILabel *)totalAmountLabel
{
    if (!_totalAmountLabel) {
        _totalAmountLabel = [TSWLTools allocLabel:@"共1件商品" font:systemFont(12) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(15), TSScaleW(14) + _bottomLine.bottom, _bacView.width - TSScaleW(15)*2, TSScaleW(12)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _totalAmountLabel;
}
-(UILabel *)totalLabel
{
    if (!_totalLabel) {
        _totalLabel = [TSWLTools allocLabel:@"合计：￥100.00" font:systemFont(12) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(15), TSScaleW(14) + _bottomLine.bottom, _bacView.width - TSScaleW(15)*2, TSScaleW(12)) textAlignment:(NSTextAlignmentRight)];
        NSString *string = _totalLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(4,  string.length - 7)];
        _totalLabel.attributedText = atts;
    }
    return _totalLabel;
}
-(UIButton *)confirmDoneBtn{
    if (!_confirmDoneBtn) {
        _confirmDoneBtn = [TSWLTools allocButtonTitle:@"去付款" font:systemFont(TSScaleW(14)) textColor:kGreenColor image:nil frame:CGRectMake(TSScaleW(260), TSScaleW(52) + _bottomLine.bottom, TSScaleW(75), TSScaleW(26)) sel:@selector(confirmDoneBtnAction:) taget:self];
        [_confirmDoneBtn setBorderWithWidth:1 andColor:kGreenColor];
        [_confirmDoneBtn setCornerRadius:_confirmDoneBtn.height/2.f];
    }
    return _confirmDoneBtn;
}
-(UIButton *)cancellBtn{
    if (!_cancellBtn) {
        _cancellBtn = [TSWLTools allocButtonTitle:@"取消订单" font:systemFont(TSScaleW(14)) textColor:kSubTxtColor image:nil frame:CGRectMake(TSScaleW(165), TSScaleW(52) + _bottomLine.bottom, TSScaleW(75), TSScaleW(26)) sel:@selector(cancellBtnAction:) taget:self];
        [_cancellBtn setBorderWithWidth:1 andColor:kGrayTxtColor];
        [_cancellBtn setCornerRadius:_confirmDoneBtn.height/2.f];
    }
    return _cancellBtn;
}

-(UIButton *)applyMoneyBackBtn{
    if (!_applyMoneyBackBtn) {
        _applyMoneyBackBtn = [TSWLTools allocButtonTitle:@"退款" font:systemFont(TSScaleW(14)) textColor:kSubTxtColor image:nil frame:CGRectMake(TSScaleW(70), TSScaleW(52) + _bottomLine.bottom, TSScaleW(75), TSScaleW(26)) sel:@selector(applyMoneyBackBtnAction:) taget:self];
        [_applyMoneyBackBtn setBorderWithWidth:1 andColor:kGrayTxtColor];
        [_applyMoneyBackBtn setCornerRadius:_applyMoneyBackBtn.height/2.f];
        
    }
    return _applyMoneyBackBtn;
}
#pragma mark  ---Action
-(void)applyMoneyBackBtnAction:(UIButton *)sender
{
    !self.moneyBackBlock?:self.moneyBackBlock();
}
-(void)confirmDoneBtnAction:(UIButton *)sender
{
    !self.ensureBtnBlock?:self.ensureBtnBlock();
}

-(void)cancellBtnAction:(UIButton *)sender
{
    !self.cancellBtnBlock?:self.cancellBtnBlock();
}

-(void)setModel:(LXMineRequstOrderListModel *)model{
    
    //订单状态 0待付款 1待发货 2待收货 3待评价 4 已完成 5退款中 6 已退款 7已取消
    _model = model;
    _cancellBtnBlock = nil;
    _ensureBtnBlock = nil;
    self.messageArray = _model.ordertailList;
    _orderNumLabel.text = [NSString stringWithFormat:@"订单编号:%@",_model.ordernum];
    _statusLabel.text = _model.orderMessageString;
    
    
   
    self.totalAmountLabel.text = [NSString stringWithFormat:@"共%ld件商品",_model.ordertailList.count];
   
    if (_model.type.integerValue == 2) {
        _totalLabel.text =  [NSString stringWithFormat:@"合计：%@积分",_model.goodsprice];
        NSString *string = _totalLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(3,  string.length - 8)];
        _totalLabel.attributedText = atts;
    }else{
        _totalLabel.text =  [NSString stringWithFormat:@"合计：￥%.2f",_model.goodsprice.doubleValue];
        NSString *string = _totalLabel.text;
        NSMutableAttributedString *atts = [[NSMutableAttributedString alloc] initWithString:string];
        [atts setAttributes:@{NSFontAttributeName:systemBoldFont(TSScaleW(15))} range:NSMakeRange(4,  string.length - 7)];
        _totalLabel.attributedText = atts;
    }
    _confirmDoneBtn.hidden = YES;
    _cancellBtn.hidden = YES;
    _applyMoneyBackBtn.hidden = YES;
    if (_model.type.integerValue == 0) {
        ///商品订单
        if (_model.state.integerValue == 1 || _model.state.integerValue == 2) {
            if (_model.state.integerValue == 1) {
                _confirmDoneBtn.hidden = NO;
                self.ensureBtnBlock = self.moneyBackBlock;
                [_confirmDoneBtn setTitle:@"退款" forState:(UIControlStateNormal)];
            }
            if (_model.state.integerValue == 2) {
                _applyMoneyBackBtn.hidden = NO;
                [_applyMoneyBackBtn setTitle:@"退款" forState:(UIControlStateNormal)];
            }
            
        }
    }
    if (_model.state.integerValue == 0) {
        _confirmDoneBtn.hidden = NO;
        self.ensureBtnBlock = self.gotoPayBlock;
        [_confirmDoneBtn setTitle:@"去付款" forState:(UIControlStateNormal)];
        _cancellBtn.hidden = NO;
        self.cancellBtnBlock = self.cancellBlock;
        [_cancellBtn setTitle:@"取消订单" forState:(UIControlStateNormal)];
    }
    if (_model.state.integerValue == 2) {
        _confirmDoneBtn.hidden = NO;
        self.ensureBtnBlock = self.ensureHuoBlock;
        [_confirmDoneBtn setTitle:@"确认收货" forState:(UIControlStateNormal)];
        _cancellBtn.hidden = NO;
        self.cancellBtnBlock = self.seeWuliuBlock;
        [_cancellBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    }
    if (_model.state.integerValue == 3) {
        _confirmDoneBtn.hidden = NO;
        self.ensureBtnBlock = self.gotoCommendBlcok;
        [_confirmDoneBtn setTitle:@"去评价" forState:(UIControlStateNormal)];
        _cancellBtn.hidden = NO;
        self.cancellBtnBlock = self.seeWuliuBlock;
        [_cancellBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    }
    if (_model.state.integerValue == 4) {
        _confirmDoneBtn.hidden = NO;
        self.ensureBtnBlock = self.deleteOrderBlock;
        [_confirmDoneBtn setTitle:@"删除" forState:(UIControlStateNormal)];
        _cancellBtn.hidden = NO;
        self.cancellBtnBlock = self.seeWuliuBlock;
        [_cancellBtn setTitle:@"查看物流" forState:(UIControlStateNormal)];
    }
    if (_model.state.integerValue == 7) {
        _confirmDoneBtn.hidden = NO;
        self.ensureBtnBlock = self.deleteOrderBlock;
        [_confirmDoneBtn setTitle:@"删除" forState:(UIControlStateNormal)];
    }
}
-(void)setMessageArray:(NSArray *)messageArray{
    _messageArray = messageArray;
    for (UIView *v  in self.uiMessageArray) {
        [v removeFromSuperview];
    }
    [self.uiMessageArray removeAllObjects];
    CGFloat top = self.septorLine.bottom;
    for (int i = 0; i <self.messageArray.count; i ++) {
        NSDictionary *dataDic = self.messageArray[i];
        BkMessageOrderView *messageView = [[BkMessageOrderView alloc]init];
        messageView.top = top;
        top += messageView.height;
        _bottomLine.top = messageView.bottom;
        messageView.isScore = _model.type.integerValue == 2;
        messageView.dataDic = dataDic;
       
        [self.bacView addSubview:messageView];
        [self.uiMessageArray addObject:messageView];
    }
    
    self.totalAmountLabel.top = TSScaleW(14) + _bottomLine.bottom;
    self.totalLabel.top = self.totalAmountLabel.top;
    self.confirmDoneBtn.top = TSScaleW(52) + _bottomLine.bottom;
    self.cancellBtn.top = self.confirmDoneBtn.top;
    _applyMoneyBackBtn.top = _cancellBtn.top;
    _bacView.height = TSScaleW(20) + _confirmDoneBtn.bottom;
    
    
   // self.totalAmountLabel.text = [NSString stringWithFormat:@"@"共1件商品""]
    
    
}
-(NSMutableArray *)uiMessageArray{
    if (!_uiMessageArray) {
        _uiMessageArray = [NSMutableArray array];
    }
    return _uiMessageArray;
}


@end
